package com.fenghongzhang.day010;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btn;

    private ContentResolver contentResolver;
    private Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn);

        //解析者
         contentResolver = getContentResolver();

        //uri不存在
         uri = Uri.parse("content://com.feng");

    }

    public void btn(View view) {



        Cursor query = contentResolver.query(uri, null, null, null, null);
        if(query != null){
            while (query.moveToNext()){
                String name = query.getString(query.getColumnIndex("name"));
                String id = query.getString(query.getColumnIndex("id"));
                Toast.makeText(this, id+name, Toast.LENGTH_SHORT).show();
            }
            query.close();
        }
    }

    public void btn1(View view) {

        ContentValues contentValues = new ContentValues();
        contentValues.put("name","21号");
        contentResolver.insert(uri,contentValues);
    }

    public void btn2(View view) {
        contentResolver.delete(uri,"id=?",new String[]{"8"});
    }

    public void btn3(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","21号");
        contentResolver.update(uri,contentValues,"id=?",new String[]{"7"});
    }
}
